﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YYTea.MOdel.Model;
using YYTea.ORM;
using YYTea.Services.IServices;
using YYTea.Services.VModel;

namespace YYTea.Services.Services
{
    public class ShoppingCarServices : IShoppingCar
    {
        private readonly YYTeaContext _context;
        public ShoppingCarServices(YYTeaContext context)
        {
            _context = context;
        }
        /// <summary>
        ///添加商品到购物车
        /// </summary>
        /// <param name="shopVM"></param>
        /// <returns></returns>
        public async Task<ShoppingCar> AddProducrToShop(ShoppingCarVModel shopVM, string Id)
        {
            if (shopVM == null)
            {
                return null;
            }
            var shopId = await _context.ShoppingCars.SingleOrDefaultAsync(s => s.UserInfoID == Id);
            if (shopId != null)
            {
                ShoppingCar shop = new ShoppingCar();
                shop.DateTime = DateTime.Now;
                var SizeCount = await _context.ShoppingCars.CountAsync();
                shop.SizeNum += SizeCount;
                shop.UserInfoID = Id;
                shop.Product = await _context.Products.FirstOrDefaultAsync(s => s.Name == shopVM.ProductName);
                _context.ShoppingCars.Add(shop);
                await _context.SaveChangesAsync();
                return shop;
            } 
            return null;
        }

        //初始化购物车
        public async Task<ShoppingCar> AddShoppingCar(ShoppingCarVModel shopVModel, string Id)
        {
            if (shopVModel == null)
            {
                return null;
            }
            var shop = new ShoppingCar();
            shop.UserInfoID = Id;
            shop.DateTime = DateTime.Now;
            //if (shopVModel.ProductName == null)
            //{
            //    shop.Products = null;
            //}
            //shop.Products = _context.Products.FirstOrDefault(s => s.Name == shopVModel.ProductName);
            await _context.AddAsync(shop);
            await _context.SaveChangesAsync();
            //return addShop;
            return shop;
        }

        public Task<ShoppingCar> DeleteShoppingCarProduct()
        {
            throw new NotImplementedException();

        }

        public async Task<ShoppingCar> DeleteShoppingCarProductById(Guid Id)
        {
            ShoppingCar shop = await _context.ShoppingCars.FirstOrDefaultAsync(s => s.Product.ProductID == Id);
            if (shop == null)
            {
                return null;
            }
            _context.ShoppingCars.Remove(shop);
            await _context.SaveChangesAsync();
            return shop;
        }

        public Task<ShoppingCar> DeleteShoppingCarProductByName(string Name)
        {
            throw new NotImplementedException();
        }

        public async Task<ShoppingCar> GetShoppingCarById(string id)
        {
            //throw new NotImplementedException();
            if (id == null)
            {
                return null;
            }
            var getshop = await _context.ShoppingCars
                //连接用户表
                .Include(s => s.UserInfos)
                //连接购物表
                .Include(s => s.Products)
                //搜索条件
                .Where(s => s.UserInfoID == id)
                //第一个数据
                .FirstOrDefaultAsync();
            return getshop;
        }
    }
}
